package com.jiangjiang.eshop.wms.stock;

import com.google.common.collect.Sets;
import com.jiangjiang.eshop.order.dto.OrderItemDTO;
import com.jiangjiang.eshop.schedule.dto.SaleDeliveryScheduleResult;
import com.jiangjiang.eshop.schedule.dto.ScheduleOrderSendOutDetailDTO;
import com.jiangjiang.eshop.wms.domain.WmsGoodsAllocationStock;
import com.jiangjiang.eshop.wms.domain.WmsGoodsAllocationStockDetail;
import com.jiangjiang.eshop.wms.domain.WmsGoodsStock;
import com.jiangjiang.eshop.wms.service.WmsGoodsAllocationStockDetailService;
import com.jiangjiang.eshop.wms.service.WmsGoodsAllocationStockService;
import com.jiangjiang.eshop.wms.service.WmsGoodsStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author Jianghaowen
 * @Date 2021/9/14
 * @Description: TODO
 * @Version V1.0
 **/
@Component
public class SubmitOrderWmsStockUpdater extends AbstractWmsStockUpdater {

    private SaleDeliveryScheduleResult saleDeliveryScheduleResult;

    @Autowired
    private WmsGoodsStockService goodsStockService;
    @Autowired
    private WmsGoodsAllocationStockService getGoodsAllocationStockService;
    @Autowired
    private WmsGoodsAllocationStockDetailService goodsAllocationStockDetailService;


    @Override
    protected void updateGoodsStock() {
        OrderItemDTO orderItem = saleDeliveryScheduleResult.getOrderItem();
        WmsGoodsStock goodsStock = goodsStockService.queryBySkuIds(Sets.newHashSet(orderItem.getGoodsId())).get(0);
        goodsStock.setAvailableStockQuantity(goodsStock.getAvailableStockQuantity() - orderItem.getPurchaseQuantity());
        goodsStock.setLockedStockQuantity(goodsStock.getLockedStockQuantity() + orderItem.getPurchaseQuantity());
        goodsStockService.updateById(goodsStock);
    }

    @Override
    protected void updateGoodsAllocationStock() {
        Long goodsSkuId = saleDeliveryScheduleResult.getOrderItem().getGoodsSkuId();
        Map<Long, WmsGoodsAllocationStock> allocationStockMap = getGoodsAllocationStockService.queryBySkuId(goodsSkuId)
                .stream()
                .collect(Collectors.toMap(WmsGoodsAllocationStock::getGoodsAllocationId, Function.identity()));

        List<WmsGoodsAllocationStock> allocationStocks = saleDeliveryScheduleResult.getPickingItems().stream().map(pickingItem -> {
            WmsGoodsAllocationStock stock = allocationStockMap.get(pickingItem.getGoodsAllocationId());
            stock.setAvailableStockQuantity(stock.getAvailableStockQuantity() - pickingItem.getPickingCount());
            stock.setLockedStockQuantity(stock.getLockedStockQuantity() + pickingItem.getPickingCount());
            return stock;
        }).collect(Collectors.toList());

        getGoodsAllocationStockService.updateBatchById(allocationStocks);

    }

    @Override
    protected void updateGoodsAllocationStockDetail() {
        List<Long> sendOutDetailIds = saleDeliveryScheduleResult.getSendOutDetails()
                .stream()
                .map(ScheduleOrderSendOutDetailDTO::getGoodsAllocationStockDetailId)
                .collect(Collectors.toList());
        Map<Long, WmsGoodsAllocationStockDetail> stockDetailMap = goodsAllocationStockDetailService.queryByIds(sendOutDetailIds).stream()
                .collect(Collectors.toMap(WmsGoodsAllocationStockDetail::getId, Function.identity()));
        List<WmsGoodsAllocationStockDetail> stockDetails = saleDeliveryScheduleResult.getSendOutDetails().stream().map(sendOutDetail -> {
            WmsGoodsAllocationStockDetail stockDetail = stockDetailMap.get(sendOutDetail.getGoodsAllocationStockDetailId());
            stockDetail.setCurrentStockQuantity(stockDetail.getCurrentStockQuantity() - sendOutDetail.getSendOutCount());
            stockDetail.setLockedStockQuantity(stockDetail.getLockedStockQuantity() + sendOutDetail.getSendOutCount());
            return stockDetail;
        }).collect(Collectors.toList());

        goodsAllocationStockDetailService.updateBatchById(stockDetails);
    }

    @Override
    public void setParameter(Object parameter) {
        saleDeliveryScheduleResult = (SaleDeliveryScheduleResult) parameter;
    }
}
